package com.ms;

import com.alibaba.nacos.api.cmdb.pojo.Entity;
import com.alibaba.nacos.api.cmdb.pojo.EntityEvent;
import com.alibaba.nacos.api.cmdb.pojo.Label;
import com.alibaba.nacos.api.cmdb.spi.CmdbService;

import java.util.*;

public class CmdbDemo implements CmdbService {
    public static final List<Map<String,String>> CMDB_DATA =new ArrayList<>();

    {
        /*  CMDB_DATA_TABLE:模拟cmdb数据库数据
         *  ip          region
         *  10.10.10.10     shanghai
         *  11.11.11.11     shanghai
         *  20.20.20.20    beijing
         *  21.21.21.21     beijing
         */
        Map<String,String> machine10=new HashMap<>();
        machine10.put("ip","10.10.10.10");
        machine10.put("region","shanghai");
        CMDB_DATA.add(machine10);

        Map<String,String> machine11=new HashMap<>();
        machine11.put("ip","11.11.11.11");
        machine11.put("region","shanghai");
        CMDB_DATA.add(machine11);

        Map<String,String> machine20=new HashMap<>();
        machine20.put("ip","20.20.20.20");
        machine20.put("region","beijing");
        CMDB_DATA.add(machine20);

        Map<String,String> machine21=new HashMap<>();
        machine21.put("ip","21.21.21.21");
        machine21.put("region","beijing");
        CMDB_DATA.add(machine21);

    }

    @Override
    public Set<String> getLabelNames() {
        return parseKeySet();
    }

    private Set<String> parseKeySet(){
        Set<String> labelNames=new HashSet<>();
        CMDB_DATA.forEach(d->{
            labelNames.addAll(d.keySet());
        });
        return labelNames;
    }

    @Override
    public Set<String> getEntityTypes() {
        return parseKeySet();
    }

    @Override
    public Label getLabel(String labelName) {
        Label label=new Label();
        label.setName(labelName);
        label.setValues(new HashSet<>());
        CMDB_DATA.forEach(d->{
            label.getValues().add(d.get(labelName));
        });
        return label;
    }

    @Override
    public String getLabelValue(String entityName, String entityType, String labelName) {
        return getEntity(entityName,entityType).getLabels().get(labelName);
    }

    @Override
    public Map<String, String> getLabelValues(String entityName, String entityType) {
        return getEntity(entityName,entityType).getLabels();
    }

    @Override
    public Map<String, Map<String, Entity>> getAllEntities() {
        Map<String, Map<String, Entity>> result=new HashMap<>();
        CMDB_DATA.forEach(d->{
            d.keySet().forEach(col->{
                if(!result.containsKey(col)){
                    result.put(col,new HashMap<>());
                }
                Entity entity=new Entity();
                entity.setName(d.get(col));
                entity.setType(col);
                entity.setLabels(d);
                result.get(col).put(d.get(col),entity);
            });
        });
        return result;
    }

    @Override
    public List<EntityEvent> getEntityEvents(long timestamp) {
        //此处是cmdb数据变更事件的实现，由于是模拟，所有没有实现。
        return null;
    }

    @Override
    public Entity getEntity(String entityName, String entityType) {
        Map<String, Map<String, Entity>> allEntities = getAllEntities();
        return allEntities.get(entityType).get(entityName);
    }
}
